package hhm.android.my

import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import androidx.databinding.DataBindingUtil
import com.alibaba.android.arouter.launcher.ARouter
import com.trello.rxlifecycle4.RxLifecycle
import com.trello.rxlifecycle4.android.ActivityEvent
import hhm.android.library.bus.annotation.BusReceiver
import hhm.android.library.utils.OnMultiClickListener
import hhm.android.my.databinding.ActivityChangePasswordBinding
import siau.android.base.FORGET_PASSWORD_ROUTE
import siau.android.base.HttpHelper
import siau.android.base.SBBaseActivity
import siau.android.http.model.ChangePasswordModel
import siau.android.library.KeyString

class ChangePasswordActivity : SBBaseActivity() {

    lateinit var dataBinding: ActivityChangePasswordBinding
    private var showOldPassword = false
    private var showPassword1 = false
    private var showPassword2 = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTitleWhiteAndTextBlank()
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_change_password)
        initTitleWithLine(getString(R.string.change_password))
        dataBinding.lifecycleOwner = this

        dataBinding.activityChangePasswordPassword1Iv.setOnClickListener {
            showPassword1 = !showPassword1
            if (showPassword1) {
                dataBinding.activityChangePasswordPassword1.transformationMethod = HideReturnsTransformationMethod.getInstance()
                dataBinding.activityChangePasswordPassword1Iv.setImageResource(R.mipmap.can_see)
            } else {
                dataBinding.activityChangePasswordPassword1.transformationMethod = PasswordTransformationMethod.getInstance()
                dataBinding.activityChangePasswordPassword1Iv.setImageResource(R.mipmap.can_not_see)
            }
            dataBinding.activityChangePasswordPassword1.setSelection(dataBinding.activityChangePasswordPassword1.text.length)
        }

        dataBinding.activityChangePasswordPassword2Iv.setOnClickListener {
            showPassword2 = !showPassword2
            if (showPassword2) {
                dataBinding.activityChangePasswordPassword2.transformationMethod = HideReturnsTransformationMethod.getInstance()
                dataBinding.activityChangePasswordPassword2Iv.setImageResource(R.mipmap.can_see)
            } else {
                dataBinding.activityChangePasswordPassword2.transformationMethod = PasswordTransformationMethod.getInstance()
                dataBinding.activityChangePasswordPassword2Iv.setImageResource(R.mipmap.can_not_see)
            }
            dataBinding.activityChangePasswordPassword2.setSelection(dataBinding.activityChangePasswordPassword2.text.length)
        }

        dataBinding.activityChangePasswordOldPasswordIv.setOnClickListener {
            showOldPassword = !showOldPassword
            if (showOldPassword) {
                dataBinding.activityChangePasswordOldPassword.transformationMethod = HideReturnsTransformationMethod.getInstance()
                dataBinding.activityChangePasswordOldPasswordIv.setImageResource(R.mipmap.can_see)
            } else {
                dataBinding.activityChangePasswordOldPassword.transformationMethod = PasswordTransformationMethod.getInstance()
                dataBinding.activityChangePasswordOldPasswordIv.setImageResource(R.mipmap.can_not_see)
            }
            dataBinding.activityChangePasswordOldPassword.setSelection(dataBinding.activityChangePasswordOldPassword.text.length)
        }

        dataBinding.activityChangePasswordSave.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                if (dataBinding.activityChangePasswordPassword1.text.toString() == dataBinding.activityChangePasswordPassword2.text.toString()) {
                    showLoadingFragment(R.id.activity_change_password_fl)
                    val r = ChangePasswordModel(dataBinding.activityChangePasswordOldPassword.text.toString(), dataBinding.activityChangePasswordPassword1.text.toString())
                    HttpHelper().changePassword(r)?.compose(RxLifecycle.bindUntilEvent(lifecycle(), ActivityEvent.STOP))?.subscribe({
                        removeLoadingFragment()
                        setResult(1024)
                        finish()
                    }, {
                        removeLoadingFragment()
                        showToast(it.message)
                    })
                } else {
                    showToast(getString(R.string.two_password_is_not_same))
                }
            }
        })

        dataBinding.activityChangePasswordForget.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                ARouter.getInstance().build(FORGET_PASSWORD_ROUTE).navigation()
            }
        })
    }

    @BusReceiver
    fun StringEvent(event: String) {
        if (event == KeyString.passwordChange) {
            setResult(1024)
            finish()
        }
    }
}